package johnkkk.com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES32;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.code.AbsGLRender;
import com.system.Camera;
import com.system.GLApplication;
import com.system.GLLog;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import johnkkk.com.gl3x.R;
import johnkkk.com.obj.Box;
import johnkkk.com.obj.FBOObj;
import johnkkk.com.obj.SkyCube;

/**
 * @Author: clm
 * @Date: 2018-10-30
 */

public class FBOTestGLRender extends AbsGLRender {
    private Camera mCamera;
    private FBOObj mFBOObj;
    public FBOTestGLRender(Context context) {
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(GL10 gl10, EGLConfig eglConfig) {
        GLES32.glEnable(GLES32.GL_DEPTH_TEST);
        //创建fbo
        mFBOObj = new FBOObj("fbo");
    }

    @Override
    protected Camera getCamera() {
        mCamera = new Camera();
        return mCamera;
    }

    @Override
    protected void onCreateRootNode() {
        //创建根节点时
        //添加摄像机
        mRootNode.addChild(mCamera);
        //添加测试物体
        Box box = new Box("box");
        mRootNode.addChild(box);
        //添加天空盒
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;//Opengl需要非压缩形式的原始数据
        List<Bitmap> bitmaps = new ArrayList<>();
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.right, options));
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.left, options));
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.top, options));
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.bottom, options));
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.front, options));
        bitmaps.add(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.back, options));
        mRootNode.addChild(new SkyCube("sky", mCamera, bitmaps));
    }

    @Override
    protected void onInitChange(GL10 gl10, int w, int h) {
        GLLog.loge("w = " + w + ";h = " + h);
        GLLog.loge("sw = " + GLApplication.sWidth + ";sh = " + GLApplication.sHeight);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onDrawFrame(GL10 gl10) {
        //绑定到fbo
        GLES32.glBindFramebuffer(GLES32.GL_FRAMEBUFFER, mFBOObj.mFBO[0]);
        super.onDrawFrame(gl10);
        //解绑fbo,绑定默认fbo
        GLES32.glBindFramebuffer(GLES32.GL_FRAMEBUFFER, 0);
        //再次渲染一次,为了加速,关闭深度测试(因为不需要进行深度测试)
        GLES32.glDisable(GLES32.GL_DEPTH_TEST);
        GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT);
        mFBOObj.render(0);
        GLES32.glEnable(GLES32.GL_DEPTH_TEST);
    }
}
